package com.hsnn.medstgmini.sys.model;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.hsnn.medstgmini.model.Status;

/**
 * @category 权限
 * @author 蔡春龙
 * @date 2015年6月12日
 */
@Entity
@Table(name = "sys_privilege")
public class SysPrivilege implements Serializable {

	private static final long serialVersionUID = 1L;

	private String id;
	private String name;
	private String menuName;
	private Integer type; // 0：系统，1：模块，2：菜单，3：按钮
	private Integer classify; // 权限分类（0：非业务，1：业务）
	private String typeName;
	private String url;
	private String uri;
	private String icon;
	private Integer status;
	private Integer sort;
	private String statusName;
	private String pid;
	private String createUserId;
	private Date createDateTime;
	private String updUser;
	private Date updDateTime;
	private String remark;
	private Integer checked;
	private boolean open;

	private List<SysPrivilege> children;

	@Id
	@Column(name = "id")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "menu_name")
	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	@Column(name = "type")
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Transient
	public String getTypeName() {
		if (type != null) {
			typeName = PrivilegeType.getName(type);
		}
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	@Column(name = "classify")
	public Integer getClassify() {
		return classify;
	}

	public void setClassify(Integer classify) {
		this.classify = classify;
	}

	@Column(name = "url")
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Transient
	public String getUri() {
		if (StringUtils.isNotBlank(url)) {
			String[] urls = url.split(";");
			if (urls.length > 0) {
				uri = System.getProperty("ctx") + urls[0];
			}
		} else {
			uri = "javascript:;";
		}
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	@Column(name = "icon")
	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	@Column(name = "status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Transient
	public String getStatusName() {
		if (status != null) {
			statusName = Status.getName(status);
		}
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	@Column(name = "sort")
	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	@Column(name = "pid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "create_user_id")
	public String getCreateUserId() {
		return createUserId;
	}

	public void setCreateUserId(String createUserId) {
		this.createUserId = createUserId;
	}

	@Column(name = "create_datetime")
	public Date getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(Date createDateTime) {
		this.createDateTime = createDateTime;
	}

	@Column(name = "upd_user")
	public String getUpdUser() {
		return updUser;
	}

	public void setUpdUser(String updUser) {
		this.updUser = updUser;
	}

	@Column(name = "upd_datetime")
	public Date getUpdDateTime() {
		return updDateTime;
	}

	public void setUpdDateTime(Date updDateTime) {
		this.updDateTime = updDateTime;
	}

	@Column(name = "remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Transient
	public List<SysPrivilege> getChildren() {
		return children;
	}

	public void setChildren(List<SysPrivilege> children) {
		this.children = children;
	}

	public void addChild(SysPrivilege privilege) {
		if (privilege != null) {
			if (children == null) {
				children = new LinkedList<SysPrivilege>();
			}
			children.add(privilege);
		}
	}

	@Column(name = "checked")
	public Integer getChecked() {
		return checked;
	}

	public void setChecked(Integer checked) {
		this.checked = checked;
	}

	@Transient
	public boolean isOpen() {
		if (type != null) {
			if (PrivilegeType.system.getKey() == type) {
				open = true;
			}
		}
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}

}